import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/assets_tool/app_colors.dart';
import 'package:tiktokadult/hj_utils/api_service/mine_service.dart';
import 'package:tiktokadult/hj_utils/global.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/global_store/store.dart';
import 'package:tiktokadult/tools_base/loading/loading_alert_widget.dart';

class VipExchangeAlert extends StatefulWidget {
  final bool isBindInviteCode; // true 绑定邀请码业务
  const VipExchangeAlert({super.key, this.isBindInviteCode = false});

  @override
  State<VipExchangeAlert> createState() => _VipExchangeAlertState();
}

class _VipExchangeAlertState extends State<VipExchangeAlert> {
  final textEditCtr = TextEditingController();

  bool get isBindInviteCode => widget.isBindInviteCode;

  void _onExChangeCode() async {
    if (textEditCtr.text.isEmpty) {
      showToast("请输入兑换码");
      return;
    }

    LoadingAlertWidget.show();
    bool ret = await MineService.postExchangeCode(textEditCtr.text);
    LoadingAlertWidget.cancel();
    if (ret == true) {
      globalStore.updateUserInfo();
      showToast("兑换成功");
      Get.back();
    }
  }

  void _onBindInviteCode() async {
    if (textEditCtr.text.isEmpty) {
      showToast("请输入邀请码");
      return;
    }

    LoadingAlertWidget.show();
    bool ret = await MineService.getProxyBind(textEditCtr.text);
    LoadingAlertWidget.cancel();
    if (ret == true) {
      globalStore.meInfo?.inviterCode = textEditCtr.text;
      globalStore.updateUserInfo();
      showToast("绑定成功");
      Get.back(result: true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Material(
        color: Colors.transparent,
        child: Container(
          margin: EdgeInsets.symmetric(horizontal: 32.w),
          padding: EdgeInsets.only(top: 27.h, bottom: 28.h),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(12),
            color: Color(0xff2E2E2E),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                isBindInviteCode ? '输入邀请码' : '输入兑换码',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 22.sp,
                  fontWeight: FontWeight.w500,
                ),
              ),
              20.h.sizeBoxH,
              Divider(height: 0.5, color: Colors.white.withOpacity(0.05)),
              22.h.sizeBoxH,
              Container(
                padding: EdgeInsets.symmetric(horizontal: 13.w, vertical: 14.h),
                margin: EdgeInsets.symmetric(horizontal: 25.w),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(7),
                  border: Border.all(color: Color(0xffD7D7D7), width: 1),
                ),
                child: TextField(
                  controller: textEditCtr,
                  style: TextStyle(color: Colors.white, fontSize: 14.sp),
                  inputFormatters: [
                    FilteringTextInputFormatter.allow(RegExp('[A-Za-z0-9]')),
                  ],
                  decoration: InputDecoration(
                    isDense: true,
                    contentPadding: EdgeInsets.zero,
                    hintText: isBindInviteCode ? "请输入邀请码(字母大写)" : '请输入兑换码（字母大写）',
                    hintStyle: TextStyle(color: Color(0xff999999), fontSize: 14.sp),
                    border: InputBorder.none,
                  ),
                ),
              ),
              27.h.sizeBoxH,
              // if (isBindInviteCode)
              //   Container(
              //     padding: EdgeInsets.only(bottom: 24),
              //     child: Text(
              //       "提示：邀请码只能绑定一次，且不能修改",
              //       style: TextStyle(
              //         color: Color(0xff999999),
              //         fontSize: 14,
              //       ),
              //     ),
              //   ),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 25.w),
                child: Row(
                  children: [
                    GestureDetector(
                      onTap: () => Get.back(),
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: 24),
                        height: 44,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: Colors.white.withOpacity(.3),
                          borderRadius: BorderRadius.circular(22),
                        ),
                        child: Text(
                          '稍后再说',
                          style: TextStyle(color: Colors.white.withOpacity(.8), fontSize: 16.sp, fontWeight: FontWeight.w400),
                        ),
                      ),
                    ),
                    14.w.sizeBoxW,
                    Expanded(
                      child: GestureDetector(
                        onTap: () async {
                          if (widget.isBindInviteCode) {
                            _onBindInviteCode();
                          } else {
                            _onExChangeCode();
                          }
                        },
                        child: Container(
                          height: 44,
                          alignment: Alignment.center,
                          child: Text(
                            isBindInviteCode ? "立即绑定" : '立即兑换',
                            style: TextStyle(color: Colors.white, fontSize: 16.sp, fontWeight: FontWeight.w500),
                          ),
                          decoration: BoxDecoration(
                            color: AppColors.primaryHighColor,
                            borderRadius: BorderRadius.circular(22),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
